package com.kye.express.business.notice.notice_center;

import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.jakewharton.rxbinding2.view.RxView;
import com.kye.base.DebounceObservableTransformer;
import com.kye.express.R;
import com.kye.express.business.notice.notice_detail.NoticeDetailActivity;
import com.kye.express.databinding.ItemNoticeBinding;
import com.kye.model.bean.notice.NotificationListResultBean;

import io.reactivex.Observable;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
import me.drakeet.multitype.ItemViewBinder;

/**
 * @author : Joe
 * @version : 1.0
 * @editor : Joe
 * @created : 26/12/2017
 * @updated : 26/12/2017
 * @description : <Description>
 * @update_reason : <UpdateReason>
 */

public class NoticeItemViewBinder extends ItemViewBinder<NotificationListResultBean.NotificationListItemBean, NoticeItemViewBinder.ViewHolder> {

    @NonNull
    @Override
    protected ViewHolder onCreateViewHolder(@NonNull LayoutInflater inflater, @NonNull ViewGroup parent) {
        View view = inflater.inflate(R.layout.item_notice, parent, false);
        return new NoticeItemViewBinder.ViewHolder(view);
    }

    @Override
    protected void onBindViewHolder(@NonNull ViewHolder holder, @NonNull NotificationListResultBean.NotificationListItemBean item) {
        holder.bindData(item);
    }

    @Override
    protected void onViewAttachedToWindow(@NonNull ViewHolder holder) {
        super.onViewAttachedToWindow(holder);

    }

    @Override
    protected void onViewDetachedFromWindow(@NonNull ViewHolder holder) {
        super.onViewDetachedFromWindow(holder);
        if (holder.getDisposable() != null && !holder.getDisposable().isDisposed()) {
            holder.getDisposable().dispose();
        }
    }

    public class ViewHolder extends RecyclerView.ViewHolder {

        private NotificationListResultBean.NotificationListItemBean data;
        private ItemNoticeBinding mDataBinding;
        Observable<Object> noticeObservable;
        Disposable disposable;

        public ViewHolder(View itemView) {
            super(itemView);
            mDataBinding = DataBindingUtil.bind(itemView);
            initEvents();
        }

        private void initEvents() {
            noticeObservable = RxView.clicks(mDataBinding.rlNoticeItem)
                    .compose(new DebounceObservableTransformer<Object>());
        }

        public void bindData(final NotificationListResultBean.NotificationListItemBean data) {
            this.data = data;
            mDataBinding.setData(data);
            disposable = noticeObservable.subscribe(new Consumer<Object>() {
                @Override
                public void accept(Object o) throws Exception {
//                    Toast.makeText(itemView.getContext(), data.toString(), Toast.LENGTH_SHORT).show();
                    Intent intent = NoticeDetailActivity.newIntent(itemView.getContext(), data.getId());
                    itemView.getContext().startActivity(intent);
                }
            });
        }

        public Observable<Object> getNoticeObservable() {
            return noticeObservable;
        }

        public Disposable getDisposable() {
            return disposable;
        }
    }
}
